package com.cq.hd.product.validator;

import com.cq.hd.product.api.dto.ActiveReleaseDto;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public abstract class ActivePublishValidator {

    static List<ActivePublishValidator> VALIDATORS = Collections.synchronizedList(new ArrayList<>());

    abstract void doValidate(ActiveReleaseDto activeReleaseDto);

    abstract Boolean match(ActiveReleaseDto activeReleaseDto);

    @PostConstruct
    public void init() {
        VALIDATORS.add(this);
    }

    public static void validate(ActiveReleaseDto activeReleaseDto) {
        VALIDATORS.forEach(
                validator -> {
                    if (validator.match(activeReleaseDto)) {
                        validator.doValidate(activeReleaseDto);
                    }
                }
        );
    }

}
